$(document).ready(function () {
    // 向后端发送请求获取医生数据
    $.ajax({
        url: '/webapi/doctor/list',
        method: 'GET'
    }).done(function (data) {
        if (data && data.rows && data.rows.length > 0) {
            console.log(data);
            // 假设返回的数据是医生列表
            // 初始化表格
            $('#registerTb').bootstrapTable({
                method: 'GET',
                data: data.rows, // 直接使用返回的数据
                striped: true,
                cache: false,
                pagination: true,
                sortable: false,
                sidePagination: "client", // 使用前端分页
                pageNumber: 1,
                pageSize: 5, // 每页显示5条数据
                pageList: [5, 10, 20],
                columns: [{
                    checkbox: true,
                }, {
                    field: 'id',
                    title: '医生ID'
                }, {
                    field: 'doctorHospital',
                    title: '所属医院'
                }, {
                    field: 'doctorDepartment',
                    title: '所在科室'
                }, {
                    field: 'doctorName',
                    title: '医生姓名'
                }, {
                    field: 'doctorNo',
                    title: '医生工号'
                }, {
                    field: 'profession',
                    title: '职称'
                }, {
                    field: 'doctorSex',
                    title: '医生性别'
                }, {
                    field: 'doctorAge',
                    title: '医生年龄'
                }, {
                    field: 'university',
                    title: '毕业院校'
                },{
                    field: 'education',
                    title: '学历'
                },{
                    field: 'major',
                    title: '专业'
                },{
                    field: 'workday',
                    title: '开始工作时间'
                },{
                    field: 'handle',
                    title: '操作',
                    formatter: operateFormatter // 自定义操作列的格式化函数
                }]
            });

            $('.fixed-table-pagination').show(); // 显示分页控件
        } else {
            alert('未找到医生信息');
        }
    }).fail(function (xhr, textStatus, errorThrown) {
        console.error('获取医生数据时出错:', textStatus, errorThrown);
        alert("无法获取医生信息，请稍后重试！");
    });
});
// 自定义操作列的格式化函数
function operateFormatter(value, row, index) {
    // 返回操作按钮的HTML代码
    return [
        '<button class="btn btn-sm btn-primary mr-2" onclick="openAppointmentModal(\'' + row.doctorDepartment + '\', ' + row.id + ', \'' + row.doctorName + '\')">预约</button>'
    ].join('');
}

// 打开预约对话框
function openAppointmentModal(doctorDepartment, doctorId, doctorName) {
    $('#appointmentModal').data('doctorDepartment', doctorDepartment); // 保存医生ID
    $('#appointmentModal').data('doctorId', doctorId); // 保存医生ID
    $('#appointmentModal').data('doctorName', doctorName); // 保存医生姓名
    $('#appointmentModal').modal('show'); // 显示模态框
}

// 确认预约
$('#confirmAppointment').click(function () {
    var doctorDepartment = $('#appointmentModal').data('doctorDepartment');
    var doctorId = $('#appointmentModal').data('doctorId');
    var doctorName = $('#appointmentModal').data('doctorName');
    var appointmentTime = $('#appointmentTime').val();

    // 发送预约请求到后端
    $.ajax({
        url: '/webapi/doctor/appointment', // 假设后端接口为 /webapi/appointment
        method: 'POST',
        contentType: 'application/json',
        data: JSON.stringify({
            doctorId: doctorId,
            doctorDepartment: doctorDepartment,
            doctorName: doctorName,
            appointmentTime: appointmentTime
        })
    }).done(function (response) {
        if (response == "预约成功") {
            alert('预约成功！');
            $('#appointmentModal').modal('hide'); // 关闭模态框
            // 可选：刷新表格或执行其他操作
        } else {
            alert('预约失败：' + response.message);
        }
    }).fail(function (xhr, textStatus, errorThrown) {
        console.error('预约请求时出错:', textStatus, errorThrown);
        alert("预约失败，请稍后重试！");
    });
});
